package com.example.sunmiv1.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.TextView;

import com.example.sunmiv1.R;
import com.example.sunmiv1.entity.ChildEntity;
import com.example.sunmiv1.entity.ParentEntity;

import java.util.ArrayList;


/**
 * @author Apathy、恆
 *         <p>
 *         父類分組的適配器
 *         <p>
 *         <br/>
 *         <br/>
 *         <p>
 *         方法 {@link #getChildView(int, int, boolean, View, ViewGroup)}<b><font
 *         color='#ff00ff' size='2'>極其重要</font></b>
 */

public class ParentAdapter extends BaseExpandableListAdapter {

    private Context mContext;// 上下文

    private ArrayList<ParentEntity> mParents;// 數據源

    private onListener onListener; //父類監聽

    private static final String TAG = "ParentAdapter";

    private OnChildTreeViewClickListener mTreeViewClickListener;// 點擊子ExpandableListView子項的監聽

    ChildAdapter childAdapter;
    GroupHolder holder = null;

    private int index;

    public ParentAdapter(Context context, ArrayList<ParentEntity> parents) {
        this.mContext = context;
        this.mParents = parents;
    }

    public void isSelect(int index){
        this.index = index;
        notifyDataSetChanged();
    }

    @Override
    public ChildEntity getChild(int groupPosition, int childPosition) {
        return mParents.get(groupPosition).getChilds().get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mParents.get(groupPosition).getChilds() != null ? mParents
                .get(groupPosition).getChilds().size() : 0;
    }

    @Override
    public View getChildView(final int groupPosition1, final int childPosition,
                             boolean isExpanded, View convertView, ViewGroup parent) {

        final ExpandableListView eListView = getExpandableListView();

        ArrayList<ChildEntity> childs = new ArrayList<ChildEntity>();

        final ChildEntity child = getChild(groupPosition1, childPosition);

        childs.add(child);

        childAdapter = new ChildAdapter(this.mContext, childs);

        eListView.setAdapter(childAdapter);

        /**
         * @author Apathy、恆
         *
         *         點擊子ExpandableListView子項時，調用回調接口
         * */
        eListView.setOnChildClickListener(new OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView arg0, View arg1,
                                        int groupIndex, int childIndex, long arg4) {
                if (mTreeViewClickListener != null) {
                    mTreeViewClickListener.onClickPosition(groupPosition1, childPosition, childIndex);
                }
                return false;
            }
        });


        /**
         * @author Apathy、恆
         *
         *         子ExpandableListView展開時，因爲group只有一項，所以子ExpandableListView的總高度=
         *         （子ExpandableListView的child數量 + 1 ）* 每一項的高度
         * */
        eListView.setOnGroupExpandListener(new OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {

                LayoutParams lp = new LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, (child
                        .getChildNames().size() + 1) * (int) mContext.getResources().getDimension(
                        R.dimen.parent_expandable_list_height));
                eListView.setLayoutParams(lp);
                Log.e(TAG, "onGroupExpand:" + childPosition + groupPosition1);
                Log.e(TAG, "onGroupExpand: size" + mParents.get(groupPosition1).getChilds().get(childPosition).getChildNames().size());
                if (mParents.get(groupPosition1).getChilds().get(childPosition).getChildNames().size() == 0) {
                    Log.e(TAG, "onGroupExpand: " + "here");
                    onListener.getPositionTwo(groupPosition1, childPosition);
                }else{
                    childAdapter.isSelect(childPosition);
                    Log.e(TAG, "onGroupExpand: 是不是經過這裏");
                }
            }
        });

        /**
         * @author Apathy、恆
         *
         *         子ExpandableListView關閉時，此時只剩下group這一項，
         *         所以子ExpandableListView的總高度即爲一項的高度
         * */
        eListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
            @Override
            public void onGroupCollapse(int groupPosition) {

                LayoutParams lp = new LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, (int) mContext
                        .getResources().getDimension(
                                R.dimen.parent_expandable_list_height));
                eListView.setLayoutParams(lp);
            }
        });
        return eListView;

    }

    /**
     * @author Apathy、恆
     * <p>
     * 動態創建子ExpandableListView
     */
    public ExpandableListView getExpandableListView() {
        ExpandableListView mExpandableListView = new ExpandableListView(
                mContext);
        LayoutParams lp = new LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, (int) mContext
                .getResources().getDimension(
                        R.dimen.parent_expandable_list_height));
        mExpandableListView.setLayoutParams(lp);
        mExpandableListView.setDividerHeight(0);// 取消group項的分割線
        mExpandableListView.setChildDivider(null);// 取消child項的分割線
        mExpandableListView.setGroupIndicator(null);// 取消展開摺疊的指示圖標
        return mExpandableListView;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return mParents.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return mParents != null ? mParents.size() : 0;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
//        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.parent_group_item, null);
            holder = new GroupHolder(convertView);
//            convertView.setTag(holder);
//        } else {
//            holder = (GroupHolder) convertView.getTag();
//        }
        holder.update(mParents.get(groupPosition));
        if (mParents.get(groupPosition).getChilds().size() == 0) {
            holder.parentGroupTV.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Log.d("測試12","執行"+"："+groupPosition+"："+mParents.get(groupPosition).getGroupName());
                    onListener.getPosition(groupPosition);
                }
            });
        }
        /*if(index==groupPosition){
            holder.parentGroupTV.setTextColor(Color.parseColor("#FF6673"));
        }else{
            holder.parentGroupTV.setTextColor(Color.parseColor("#333333"));
        }*/
        return convertView;
    }

    public void onClicked(onListener onListener) {
        this.onListener = onListener;
    }

    public interface onListener {
        void getPosition(int position);

        void getPositionTwo(int groupPosition, int childPosition);
    }

    /**
     * @author Apathy、恆
     *         <p>
     *         Holder優化
     */
    class GroupHolder {

        private TextView parentGroupTV;

        public GroupHolder(View v) {
            parentGroupTV = (TextView) v.findViewById(R.id.parentGroupTV);
        }

        public void update(ParentEntity model) {
            parentGroupTV.setText(model.getGroupName());
//            parentGroupTV.setTextColor(Color.parseColor("#FF6673"));
        }
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    /**
     * @author Apathy、恆
     * <p>
     * 設置點擊子ExpandableListView子項的監聽
     */
    public void setOnChildTreeViewClickListener(
            OnChildTreeViewClickListener treeViewClickListener) {
        this.mTreeViewClickListener = treeViewClickListener;
    }

    /**
     * @author Apathy、恆
     *         <p>
     *         點擊子ExpandableListView子項的回調接口
     */
    public interface OnChildTreeViewClickListener {

        void onClickPosition(int parentPosition, int groupPosition,
                             int childPosition);
    }

}

